package com.wanwan.service.impl;

import com.wanwan.service.ReptileService;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.stereotype.Service;
import utils.ConnectHttps;

import java.io.File;
import java.io.PrintWriter;

/**
 * 爬取优文网
 * @author xzx
 */
@Service
public class ReptileServiceImpl implements ReptileService {


    @Override
    public boolean runReptile() {
        try {
            //信任所有
            ConnectHttps.trustEveryone();
            Connection connect =  Jsoup.connect("http://lol.uuu9.com/hero.shtml");
            connect.header("Sec-Fetch-User","?1");
            connect.header("Upgrade-Insecure-Requests","1");
            connect.header("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36");
            Document document = connect.get();
            Element element = document.select("div.content").first();
            String content = element.text();
            File file = new File("D:\\年度总结.txt");
            PrintWriter writer = new PrintWriter(file);
            writer.print(content);
            writer.flush();
            writer.close();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean runReptileBall() {
        try {
            Document document = Jsoup.connect("http://www.0551dqwx.com/25102.html").get();
            Element element = document.select(".PostContent").first();
            String text = element.text();
            File file = new File("D:\\金句.txt");
            PrintWriter printWriter = new PrintWriter(file);
            printWriter.print(text);
            printWriter.flush();
            printWriter.close();
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
